<?php
namespace App\Api\V1\Models;

use Illuminate\Database\Eloquent\Model;
use think\facade\Cache;


class Admin extends Model
{

    protected $table = 'admin';
    public $timestamps = false;
//    protected $primaryKey= 'admin_id';

    /**
     * 添加一个账号
     * @access public
     * @param array $data 外部数据
     * @return array|bool
     */
    public function addAdminItem(array $data)
    {
        $data = [
            "username"=> "李彦鹏",
            "password"=> "123456",
            "group_id"=> 1,
            "nickname"=> "我是一个小程序员",
            "head_pic"=> "//host/head_pic.jpg",
            "create_time"=> time(),
            "update_time"=> time(),
        ];
        /*
         * 添加到数据库中
         * $ad = new Admin();
         * $ad->username = $data['username'];
         * $ad->password = $data['password'];
         * $ad->group_id = $data['group_id'];
         * $ad->nickname = $data['nickname'];
         * $ad->head_pic = $data['head_pic'];
         * $ad->create_time = $data['create_time'];
         * $ad->update_time = $data['update_time'];
         */
        $this->username = $data['username'];
        $this->password = $data['password'];
        $this->group_id = $data['group_id'];
        $this->nickname = $data['nickname'];
        $this->head_pic = $data['head_pic'];
        $this->create_time = $data['create_time'];
        $this->update_time = $data['update_time'];
        if ($this->save()){
            return $this->toArray();
        }

        return false;
    }

    /**
     * 编辑一个账号
     * @access public
     * @param array $data 外部数据
     * @return array|false
     */
    public function setAdminItem(array $data)
    {
        $ds = [
            "admin" => 'adminalll',
            "password" => '123456',
            "update_time"=> time()
        ];
        $res = admin::where('admin_id',14)->update($ds);
        if ($res){
            return true;
        }
        return false;
    }


    /**
     * 修改一个账号密码
     */
    public function setAdminPassword($ds)
    {
        $ds = [
            "group_id" => 2,
            "password" => '123456',
            "update_time"=> time()
        ];
        $res = admin::where('admin_id',14)->update($ds);
        if ($res){
            return $res;
        }
        return false;
    }

    /**
     * 获取一个账号
     * @access public
     * @param array $data 外部数据
     * @return array|false
     * @throws
     */
    public function getAdminItem(array $data)
    {

        $res = admin::where('admin_id',14)->first()->toArray();
        if ($res){
            return $res;
        }

    }
    /**
     * 获取账号列表
     * @access public
     * @param array $data 外部数据
     * @return array|false
     * @throws
     */
    public function getAdminList(array $data)
    {
        // 实际查询
        $res = admin::select()->limit('10');

        return json_encode($res);
    }


    /**
     * 批量删除账号
     * @access public
     * @param array $data 外部数据
     * @return array|false
     * @throws
     */
    public function delAdminList(array $data)
    {
        $admin_id = [21,22,23,24,25,26];
        $res = admin::where('admin_id','$admin_id')->delete();
        if ($res){
            return $res;
        }
    }
}
